<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class staticpage extends CI_Controller{
      protected $_templates;
      function __construct() {
		parent::__construct();
		$this->pre_message = "";
		$this->imagemanager_rootpath = 'static';       
		$this->session->set_userdata(array('Url'=>uri_string()));
		$this->load->model('staticpage_model','staticpage');
		$this->lang->load('staticpage'); 
		$this->load->library('string_library');
      }   
	  function index(){
		  
		  $this->listStaticpage();
	  }
      function listStaticpage(){
            $data['title'] = 'Danh sách trang tĩnh';
            // $data['add'] = 'static/add';
            $config['base_url'] = base_url().'static/liststatic/'.(int)$this->uri->segment(3);  
            $data['num'] = $this->staticpage->get_num_data();
           	$config['total_rows']   =  $data['num'];
         	$config['per_page']  =   '20';
           	$config['uri_segment'] = 4;   
            $this->pagination->initialize($config);
            $data['list'] =   $this->staticpage->get_all_data($config['per_page'],(int)$this->uri->segment('4'));
            $data['pagination']    = $this->pagination->create_links();       
            $this->_templates['page'] = 'static/liststatic';
            $this->site_lib->load($this->_templates['page'],$data); 
      }
    
    
       function edit($id){                       
            $data['title'] = 'Sửa trang tĩnh';
            $data['query'] = $this->staticpage->get_object($id);
			$this->form_validation->set_rules('static_content',lang('static_content'),'trim|required');
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
				 
				$data = $this->build_data($_POST,1);
				 if($this->staticpage->saveData($id,$data)){
					 $this->session->set_flashdata('message','Lưu thành công');
					 redirect('staticpage/liststaticPage');
				 }else{
					 $this->pre_message = 'Lưu không thành công';
				}
		 }
          $data['message'] = $this->pre_message;
		  $data['imagemanager_rootpath'] = $this->imagemanager_rootpath;
          $this->_templates['page'] = 'static/edit';
          $this->site_lib->load($this->_templates['page'],$data);
      }
      
   
    function build_data($data,$flag=1)
	{//flag=1:0?insert:update
			$dataUpdate["static_title"]=trim($data["static_title"]);
			/*$dataUpdate["static_alias"]=$this->string_library->stringchange($data["static_title"]);*/
			$dataUpdate["static_content"]=trim($data["static_content"]);

			if(isset($data["bl_active"])){
			$dataUpdate["bl_active"]=trim($data["bl_active"]);
		}else{
			$dataUpdate["bl_active"]=0;
		}
		return $dataUpdate;
	}

  }
?>
